package com.seagull.sb.service;

import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.SecurityContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.seagull.sb.bo.LeaveBO;
import com.seagull.sb.domain.Leave;
import com.seagull.sb.dto.LeaveDto;
import com.seagull.sb.util.SBRequestContext;
import com.seagull.sb.util.ServiceMapper;

@Component
@Path("/leave")
public class LeaveService extends BaseService {

	@Autowired 
	LeaveBO leaveBO;
	
	private ServiceMapper sm = ServiceMapper.getInstaMapper();

	@GET
	@Path("/")
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"teacher"})
	public List<LeaveDto> getLeave(@Context SecurityContext sc) {
		
		SBRequestContext rc = populateRequestContext(sc); 
		List<Leave> leaves = leaveBO.getLeaves(rc,new LeaveDto());
		return sm.mapToLeaveDto(leaves);
	}
	
	@POST
	@Path("/send")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"student"})
	public LeaveDto send(@Context SecurityContext sc, LeaveDto leaveDto) {
		
		SBRequestContext rc = populateRequestContext(sc); 
		
		Leave leave = sm.mapToLeave(leaveDto);
		leaveBO.send(rc, leave);
		System.out.println("saveLeave - Successful.");
		return leaveDto;
 	}
	
	@POST
	@Path("/authorize")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@RolesAllowed({"teacher"})
	public LeaveDto authorize(@Context SecurityContext sc, LeaveDto leaveDto) {
		
		SBRequestContext rc = populateRequestContext(sc); 
		
		Leave leave = sm.mapToAuthorizeLeave(leaveDto);
		
		leaveBO.authorize(rc, leave);
		System.out.println("updateLeave - Successful.");
		return leaveDto;
 	}
	
}